/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once
#include "Hogshead.Common.h"
#include "../Content/Texture.h"
#include "../Directors/IProcessInfo.h"
namespace Hogshead
{
	namespace DX11Rendering
	{
		class ParticleBufferInfo : public IProcessInfo
		{
			
		public:
			int _num_particles;
			int _particle_size;
	
			bool _first_run;

			bool _already_updated;

			Texture* getRandomTexture()
			{
				return _random_texture;
			}

			Texture* getParticleTexture()
			{
				return _particle_texture;
			}

			ID3D11Buffer* getInitBuffer()
			{
				return _init_buffer;
			}

			ID3D11Buffer* getDrawBuffer()
			{
				return _draw_buffer;
			}

			ID3D11Buffer* getStreamOutBuffer()
			{
				return _stream_out_buffer;
			}

			void setInitBuffer(ID3D11Buffer* _buffer)
			{
				if(_init_buffer)
					_init_buffer->Release();
				_init_buffer = _buffer;
			}

			void setDrawBuffer(ID3D11Buffer* _buffer)
			{
				if(_draw_buffer)
					_draw_buffer->Release();
				_draw_buffer = _buffer;
			}

			void setStreamOutBuffer(ID3D11Buffer* _buffer)
			{
				if(_stream_out_buffer)
					_stream_out_buffer->Release();
				_stream_out_buffer = _buffer;
			}

			void setRandomTexture(Texture* _tex)
			{
				if(_random_texture)
					Factory::release(_random_texture);
				_random_texture = _tex;
			}

			void setParticleTexture(Texture* _tex)
			{
				if(_particle_texture)
					ContentLibrary::release(_particle_texture);
				_particle_texture = _tex;
			}

			ParticleBufferInfo()
			{
				_init_buffer= NULL;
				_num_particles = 0;
				_particle_size = 0;
				_random_texture = NULL;
				_draw_buffer = NULL;
				_stream_out_buffer = NULL;
				_particle_texture = NULL;
				_first_run = true;

				_already_updated = false;
			}

			~ParticleBufferInfo()
			{
				if(_init_buffer)
					_init_buffer->Release();

				if(_draw_buffer)
					_draw_buffer->Release();

				if(_stream_out_buffer)
					_stream_out_buffer->Release();

				if(_random_texture)
					Factory::release(_random_texture);

				if(_particle_texture)
					ContentLibrary::release(_particle_texture);
			}

			IProcessInfo* clone() const
			{
				ParticleBufferInfo* clone = new ParticleBufferInfo();
				clone->_num_particles = this->_num_particles;
				clone->_particle_size = this->_particle_size;

				clone->_first_run = this->_first_run;

				clone->_random_texture = NULL;

				if(this->_particle_texture)
					clone->_particle_texture = ContentLibrary::getContent<Texture>(this->_particle_texture->getKey());

				clone->_already_updated = false;

				return clone;
			}

		private:
			friend class ParticleProcess;

			Texture* _random_texture;

			Texture* _particle_texture;

			ID3D11Buffer* _init_buffer;
			ID3D11Buffer* _draw_buffer;
			ID3D11Buffer* _stream_out_buffer;

			
		};

		struct Particle
		{
			Vector3 _pos;
			Vector3 _velocity;
			Vector3 _accel;
			Vector2 _size;
			float _lifetime;
			float _age;
			float _rotation;
			UINT _type;
			UINT _ID;
			
			Particle()
			{
				_pos = _velocity = _accel = Vector3(0,0,0);
				_size = Vector2(0,0);
				_lifetime = _age = 0.0f;
				_ID = 0;
				_type = 0;
			}

			Particle(Vector3 in_pos,
				Vector3 in_velocity,
				Vector3 in_accel,
				Vector2 in_size,
				float in_lifetime,
				float in_age,
				float in_rotation,
				UINT in_type,
				UINT in_ID)
			{
				_pos = in_pos;
				_velocity = in_velocity;
				_accel = in_accel;
				_size = in_size;
				_lifetime = in_lifetime;
				_age = in_age;
				_rotation = in_rotation;
				_type = in_type;
				_ID = in_ID;
			}

			bool operator==(const Particle& other) const
			{
				return	_pos == other._pos &&
						_velocity == other._velocity &&
						_accel == other._accel &&
						_size == other._size &&
						_lifetime == other._lifetime &&
						_age == other._age &&
						_rotation == other._age &&
						_type == other._type &&
						_ID == other._ID;
			}
		};
	}
}